package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.OverflowListGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * ClassName: OverflowListGoodsServiceImpl
 * Package: com.atguigu.jxc.service.impl
 * Description:
 *
 * @Author 梅超凡
 * @Create 2025/2/22 16:22
 * @Version 1.0
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {


    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    @Autowired
    private UserDao userDao;


    @Override
    public ServiceVO<Object> saveOverFlowListGoods(String overflowNumber, String overflowDate, String overflowListGoodsStr, String remarks, User user) {

        OverflowList overflowList = new OverflowList();
        overflowList.setOverflowNumber(overflowNumber);
        overflowList.setOverflowDate(overflowDate);
        overflowList.setRemarks(remarks);
        overflowList.setUserId(user.getUserId());
        int i = overflowListGoodsDao.saveOverflowList(overflowList);
        AtomicInteger count = new AtomicInteger();
        Integer overflowListId = overflowListGoodsDao.getOverflowListId(overflowNumber);
        //通过手写方法将damageListGoodsStr转换成List<OverflowListGoods>
        List<OverflowListGoods> overflowListGoodsList = getOverflowListGoodsList(overflowListGoodsStr);
        overflowListGoodsList.forEach(overflowGoods -> {
            OverflowListGoods overflowListGoods = new OverflowListGoods();
            BeanUtils.copyProperties(overflowGoods, overflowListGoods);
            overflowListGoods.setOverflowListId(overflowListId);
            int saved = overflowListGoodsDao.saveOverflowListGoods(overflowListGoods);
            if (saved > 0) {
                count.incrementAndGet();
            }
        });
        if (i > 0 && overflowListGoodsList.size() == count.get()){
            return new ServiceVO<Object>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, overflowListGoodsList);
        }
        return new ServiceVO<Object>(ErrorCode.ADD_ERROR_CODE,ErrorCode.ADD_ERROR_MESS, overflowListGoodsList);
    }

    @Override
    public Map<String, Object> listOverFlowList(String sTime, String eTime) {
        Map<String, Object> resultMap = new HashMap<>();
        // 根据时间查询报损单商品
        List<OverflowList> overflowListGoods = overflowListGoodsDao.listOverflowList(sTime, eTime);
        if (overflowListGoods != null) {
            overflowListGoods.forEach(goods -> {
                User userById = userDao.getUserById(goods.getUserId());
                goods.setTrueName(userById.getTrueName());
            });
            resultMap.put("rows", overflowListGoods);
            return resultMap;
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> goodsList(Integer overflowListId) {
        Map<String, Object> resultMap = new HashMap<>();
        List<OverflowListGoods> overflowListGoods = overflowListGoodsDao.getOverflowListGoodsByDamageListId(overflowListId);
        if (overflowListGoods != null) {
            resultMap.put("rows", overflowListGoods);
            return resultMap;
        }
        return resultMap;
    }

    private List<OverflowListGoods> getOverflowListGoodsList(String overflowListGoodsStr) {
        //手写方法将overflowListGoodsStr转换成List<OverflowListGoods>
        List<OverflowListGoods> overflowListGoodsList = new ArrayList<>();
        // 1. 去除外层方括号
        String content = overflowListGoodsStr.substring(1, overflowListGoodsStr.length() - 1).trim();
        // 2. 分割多个对象（本例中只有一个）
        String[] objects = content.split("\\},\\s*\\{"); // 处理多个对象的情况
        for (String objStr : objects) {
            // 3. 处理单个对象
            objStr = objStr.replaceAll("[{}]", "").trim();
            String[] pairs = objStr.split(",");

            OverflowListGoods goods = new OverflowListGoods();
            for (String pair : pairs) {
                // 4. 分割键值对
                String[] keyValue = pair.split(":", 2);
                String key = keyValue[0].trim().replaceAll("\"", "");
                String value = keyValue[1].trim().replaceAll("\"", "");

                // 5. 映射到Java对象属性
                switch (key) {
                    case "goodsId":
                        goods.setGoodsId(Integer.parseInt(value));
                        break;
                    case "goodsTypeId":
                        goods.setGoodsTypeId(Integer.parseInt(value));
                        break;
                    case "goodsCode":
                        goods.setGoodsCode(value);
                        break;
                    case "goodsName":
                        goods.setGoodsName(value);
                        break;
                    case "goodsModel":
                        goods.setGoodsModel(value);
                        break;
                    case "goodsUnit":
                        goods.setGoodsUnit(value);
                        break;
                    case "lastPurchasingPrice":
                        break;
                    case "price":
                        goods.setPrice(Double.parseDouble(value));
                        break;
                    case "goodsNum":
                        goods.setGoodsNum(Integer.parseInt(value));
                        break;
                    case "total":
                        goods.setTotal(Double.parseDouble(value));
                        break;
                }
            }
            overflowListGoodsList.add(goods);
        }
        return overflowListGoodsList;
    }
}
